/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午1:13:32
 * VersionUtil.java
 * 
 */
package com.snda.game.legend.base;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author qixb.fnst
 *         <p>
 *         版本工具类
 *         </p>
 */
public final class VersionUtils {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(VersionUtils.class);
	private static Properties props = null;
	static {
		props = new Properties();
		InputStream in = null;
		try {
			in = VersionUtils.class.getResourceAsStream("version.properties");
			props.load(in);
		} catch (IOException e) {
			logger.error("读取version.properties错误");
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				logger.error("version.properties不能正常关闭");
				e.printStackTrace();
			}
		}
	}

	private static String version;
	private static String downloadUrl;
	public static boolean setVersionAndUrl(String version, String url) {
		VersionUtils.version = version;
		VersionUtils.downloadUrl = url;
		props.setProperty("currentVersion", version);
		props.setProperty("downloadUrl", url);
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(VersionUtils.class.getResource(
					"version.properties").getPath());
			props.store(outputStream, "版本更新！");

		} catch (FileNotFoundException e) {
			logger.error("读取version.properties错误");
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			logger.error("存储版本信息失败！");
			e.printStackTrace();
			return false;
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error("关闭输出流失败！");
					e.printStackTrace();
					return false;
				}
			}
		}
		return true;

	}
	public static String getVersion() {
		if (version == null) {
			version = props.getProperty("currentVersion");
		}
		return version;
	}

	public static String getdownloadUrl() {
		if (downloadUrl == null) {
			downloadUrl = props.getProperty("downloadUrl");
		}
		return downloadUrl;
	}
}
